package com.teamawesome.hackathon;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ResultParser {

	private JSONObject mainJsonObject;
	
	private ArrayList<String> headings;
	private ArrayList<String> urls;
	private ArrayList<String> images;
	private ArrayList<String> price;
	
	
	public ResultParser(String resultAsJson) {
		try {
			mainJsonObject = null;
			headings = new ArrayList<String>();
			urls = new ArrayList<String>();
			images = new ArrayList<String>();
			price = new ArrayList<String>();
			
			mainJsonObject = new JSONObject(resultAsJson);
			JSONArray results = mainJsonObject.getJSONArray("results");
			for(int i = 0; i < results.length(); i++) {
				JSONObject listing = results.getJSONObject(i);
				headings.add(listing.getString("heading"));
				urls.add(listing.getString("externalURL"));
				
				JSONArray imageArray = listing.getJSONArray("images");
				images.add(imageArray.optString(0));
				price.add(listing.getString("price"));
				
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String [] toStringArray(List<String> stringList) {
		return stringList.toArray(new String [stringList.size()]);
	}

	public String [] price(){
		return toStringArray(price);
	}
	
	public String [] titles() {
		return toStringArray(headings);

	}
	
	public String [] urls() {
		return toStringArray(urls);
	}
		
	public String [] images() {
		return toStringArray(images);
	}
}


